var app = getApp();

Page({
    data: {
        haibao: false,
        getUseInfo: false,
         is_hy: 0,
        uid: "",
        f_id: "",
        deposit: "",
        userinfo: "",
        u_id: "",
        dialog_ewm:'',
        apply_code:'',
        inviteCode:'',
        isSubscribe:false,
         widths: 300,
        heigths: 400,
    },
    onLoad: function(t) {
       var e = this;
       e.getBase(), e.getUser(),e.shopReturn(t.s_id);
       e.pushInviteCode(t.code);
       e.getOrderInfo();
       e.guideOpenSubscribeMessage();
    },
    getOrderInfo:function(){
        var self = this;
         if(wx.getStorageSync("useInfo") !='') {
            var i = !1;
            self.setData({
                getUseInfo: i
            });
         } else {
           var i = !0;
            self.setData({
                getUseInfo: i
            });
        }
    },
  closeSubscribe:function(){
    var self = this;
     self.setData({
                haibao: false
                });
  },
 setClock:function(){
    let that = this;
    console.log("消息订阅");
    var pushReservationTmplIds = 'wFtl9eZ5G5AUrAQ_0VkZFPDfXKQyAP2wrXckaQZRgRA';
    if (wx.requestSubscribeMessage) {
      wx.requestSubscribeMessage({
        tmplIds: ['Ujzc1t5qLe1oV_tpVuj3prenIdMonbdI7r9oHD23jTM','wFtl9eZ5G5AUrAQ_0VkZFGNijQGHsHTcPqWvm1YCczg'],
        success(res) {
          if (res['Ujzc1t5qLe1oV_tpVuj3prenIdMonbdI7r9oHD23jTM','wFtl9eZ5G5AUrAQ_0VkZFGNijQGHsHTcPqWvm1YCczg'] === 'accept') {
          } else if (res['Ujzc1t5qLe1oV_tpVuj3prenIdMonbdI7r9oHD23jTM','wFtl9eZ5G5AUrAQ_0VkZFGNijQGHsHTcPqWvm1YCczg'] === 'reject') {
            that.guideOpenSubscribeMessage();
          } else {
            wx.showToast({
              title: '授权订阅消息有误',
              icon: 'none'
            });
          }
        },
        fail(res) {
          // 20004:用户关闭了主开关，无法进行订阅,引导开启
          if (res.errCode == 20004) {
            // console.log(res, 'fail:用户关闭了主开关，无法进行订阅,引导开启---');
            that.guideOpenSubscribeMessage();
          }
        }
      });
    } else {
      wx.showToast({
        title: '请更新您微信版本，来获取订阅消息功能',
        icon: 'none'
      });
    }
  },
  guidSubscribeMessageAuthAfter:function() {
    //引导用户 开启订阅消息 之后，「openSetting」 接口暂时不会返回，用户手动设置后的状态，所以采用「getSetting」接口重新进行查询
   var pushReservationTmplIds = 'wFtl9eZ5G5AUrAQ_0VkZFPDfXKQyAP2wrXckaQZRgRA';
   var self = this;
   console.log("用户同意！！")
    wx.getSetting({
      withSubscriptions: true,
      success: res => {
        let {
          authSetting = {},
          subscriptionsSetting: { mainSwitch = false, itemSettings = {} } = {}
        } = res;
        console.log("用户同意res！！"+authSetting['scope.subscribeMessage']+"==>>"+mainSwitch+'===>>'+itemSettings[pushReservationTmplIds]);

        if ((authSetting['scope.subscribeMessage'] || mainSwitch) && itemSettings[pushReservationTmplIds] === 'accept') {
            console.log("用户同意accept！！");
           self.setData({
                haibao: false
                });
          // console.log('用户手动开启同意了，订阅消息');
        } else {
          wx.showToast({
           title: '您没有同意授权订阅消息，预约领取失败',
           icon: 'none'
          });
          setTimeout(function(){
             self.setData({
                 haibao: true
                });
          },1000)
        }
      }
    });
},
  guideOpenSubscribeMessage:function() {
    var _this = this;
      wx.getSetting({
      withSubscriptions: true,
      success(res) {
        var itemSettings = res.subscriptionsSetting.itemSettings;
        if (itemSettings) {
          if (itemSettings['wFtl9eZ5G5AUrAQ_0VkZFPDfXKQyAP2wrXckaQZRgRA']=='accept') {
            _this.setData({
              haibao: true
            });
          }
        }
      }
    })
  },
    pushInviteCode:function(inviteCode){
        var self = this;
        if(typeof inviteCode === 'undefined' || inviteCode == ''){
            return;
        }else{
            app.util.request({
            url: "entry/wxapp/FenxiaoDo",
            data: {
                   code: inviteCode,
                   u_id:uid
                  },
            success: function(t) {
                console.log("二维码数据"+t);
            }
         });
        }
    },
    getBase: function() {
        var e = this;
        app.util.request({
            url: "entry/wxapp/Base",
            success: function(t) {
                console.log(t), e.setData({
                    base: t.data.data
                });
            }
        });
    },
    // 一键关注公众号
    focusGzh:function(){
        var self = this;
        
    },
    // 商户转移
    shopReturn:function(e){
        var self = this;
         app.util.request({
            url: "entry/wxapp/Shangjiaqianyi",
            data:{
                 openid:wx.getStorageSync("openid"),
                 s_id:e
            },
            success: function(t) {
                console.log("数据传输"+t);
            }
        });
    },
    closemodal: function(t) {
        this.setData({
            getUseInfo:false
        });
    },
    onGotUserInfo: function(a) {
        var self = this;
        console.log("点击同意按钮提示信息"+JSON.stringify(a)), "getUserInfo:ok" == a.detail.errMsg ? (wx.setStorageSync("useInfo", "true"), 
        self.getGetUid(a.detail.userInfo), self.setData({
            getUseInfo: !1
        })) : self.setData({
            getUseInfo: !0
        });
       setTimeout(function(){
         self.getUser();
       },500)
    },
    getGetUid: function(t) {
        var e = this;
        wx.login({
            success: function(a) {
                a.code && app.util.request({
                    url: "entry/wxapp/GetUid",
                    data: {
                        code: a.code
                    },
                    success: function(s) {
                        s.data.errno || (wx.setStorageSync("openid", s.data.data.openid), console.log(s.data.data.openid), 
                        console.log(t), app.util.request({
                            url: "entry/wxapp/TyMember",
                            data: {
                                u_name: t.nickName,
                                u_thumb: t.avatarUrl,
                                u_sex: t.gender,
                                openid: s.data.data.openid
                            }
                        })), wx.getLocation({
                            type: "wgs84",
                            success: function(a) {
                                var t = a.longitude, e = a.latitude, n = a.speed, i = a.accuracy;
                                app.util.request({
                                    url: "entry/wxapp/Weizhi",
                                    cachetime: "30",
                                    data: {
                                        latitude: e,
                                        longitude: t,
                                        openid: s.data.data.openid,
                                        speed: n,
                                        accuracy: i
                                    }
                                });
                            }
                        }), app.util.request({
                            url: "entry/wxapp/User",
                            data: {
                                openid: s.data.data.openid
                            },
                            success: function(a) {
                                e.setData({
                                    is_hy: a.data.data.u_hy
                                });
                            }
                        });
                    }
                });
            }
        });
    },
  applyDialogModal1:function(event){
      const self = this;
      var fid = event.currentTarget.dataset.f_id;
        app.util.request({
            url: "entry/wxapp/getcode",
            data: {
                openid: wx.getStorageSync("openid"),
                f_id:fid
            },
            success: function(a) {
                  self.setData({
                    apply_code:a.data.data,
                    ScanCard:true
                })
            }
        });
    },
    // 获取推广码
    codeShow:function(event){
     var self = this;
     var uid = event.currentTarget.dataset.uid;
     app.util.request({
            url: "entry/wxapp/ShangjiaCode",
            data: {
                openid: wx.getStorageSync("openid")
            },
            success: function(a) {
                console.log("二维码消息"+JSON.stringify(a));
                  self.setData({
                    ScanCard:true,
                    apply_code:a.data.data
                })
            }
        });
    },
    getUser: function() {
        var e = this;
        app.util.request({
            url: "entry/wxapp/User",
            data: {
                openid: wx.getStorageSync("openid")
            },
            success: function(t) {
                console.log(wx.getStorageSync("openid")), console.log(t), e.setData({
                    userinfo: t.data.data
                });
            }
        });
    },
    btpScan:function(){
        wx.scanCode({
        success (res) {
            wx.navigateTo({
                url: decodeURIComponent(res.path)
            });
          }
      })
    },
    caozuozhinan: function(t) {
        var e = t.currentTarget.dataset.url;
        wx.navigateTo({
            url: "../outerlink/outerlink?url=" + e
        });
    },
    myPingClick: function(t) {
        this.getform(t.detail.formId), wx.navigateTo({
            url: "/hyb_jianzhi/my_ping/my_ping"
        });
    },
    applyDistrict: function(t) {
        wx.navigateTo({
            url: "/hyb_jianzhi/my_district/my_district"
        });
    },
    lockpoint:function(t) {
        this.getform(t.detail.formId), wx.navigateTo({
            url: "/hyb_jianzhi/my_point/my_point"
        });
    },
    editClick: function(t) {
        this.getform(t.detail.formId), wx.navigateTo({
            url: "/hyb_jianzhi/edit/edit"
        });
    },
    applyClick: function(t) {
        this.getform(t.detail.formId);
        var e = t.currentTarget.dataset.index;
        wx.navigateTo({
            url: "/hyb_jianzhi/apply/apply?index=" + e
        });
    },
    walletClick: function(t) {
        this.getform(t.detail.formId), wx.navigateTo({
            url: "/hyb_jianzhi/wallet/wallet"
        });
    },
    myworks:function(t){
       this.getform(t.detail.formId), wx.navigateTo({
            url: "/hyb_jianzhi/myworks/myworks"
        }); 
    },
    callPhone: function(t) {
        this.getform(t.detail.formId), wx.makePhoneCall({
            phoneNumber: t.currentTarget.dataset.phone
        });
    },
    shopClick: function(t) {
        this.getform(t.detail.formId), wx.navigateTo({
            url: "/hyb_jianzhi/shop/shop"
        });
    },
    help: function(t) {
        this.getform(t.detail.formId), wx.navigateTo({
            url: "/hyb_jianzhi/yue/yue"
        });
    },
    btpshopEnter:function(t){
        this.getform(t.detail.formId), wx.navigateTo({
            url: "/hyb_jianzhi/shop_edit/shop_edit"
        });
    },
     btpCcus:function(t){
        this.getform(t.detail.formId), wx.navigateTo({
            url: "/hyb_jianzhi/ccus/ccus"
        });
    },
    shopClick2: function(t) {
        this.getform(t.detail.formId), wx.showToast({
            title: "审核中",
            image: "/hyb_jianzhi/images/errors.png"
        });
    },
    shopClick3: function(t) {
        this.getform(t.detail.formId), wx.showToast({
            title: "请先入驻",
            image: "/hyb_jianzhi/images/errors.png"
        }), setTimeout(function() {
            wx.navigateTo({
                url: "/hyb_jianzhi/shop_edit/shop_edit?typs=ruzhu"
            });
        }, 1e3);
    },
    collect: function(t) {
        this.getform(t.detail.formId), wx.navigateTo({
            url: "../collect/collect"
        });
    },
    sell: function(t) {
        this.getform(t.detail.formId), wx.navigateTo({
            url: "/hyb_jianzhi/sell/sell"
        });
    },
    getform: function(t) {
        console.log(t), app.util.request({
            url: "entry/wxapp/Formid",
            data: {
                openid: wx.getStorageSync("openid"),
                formid: t
            },
            success: function(t) {}
        });
    },
    addvip: function(t) {
        this.getform(t.detail.formId), wx.navigateTo({
            url: "../addvip_user/addvip_user"
        });
    },
    applyDaili:function(){
        var self = this;
         wx.navigateTo({
            url: "../applyShopAgent/applyShopAgent"
        });
    },
    onReady: function() {},
    onShow: function() {
         var _this = this;
        _this.getBase(), _this.getUser();

    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});